package earthdawnnet.util;

/**
 * Simple Mutex class
 * @author Joscha
 */
public class Mutex {
	/**
	 * The mutexes state: locked or unlocked.
	 */
	private boolean locked = false;

	/**
	 * Call this method to request access on the Mutexes exclusion section. Call
	 * will block until access is acquired.
	 */
	public synchronized void acquire() {
		while (locked)
			try {
				wait();
			} catch (InterruptedException e) {
			}
		locked = true;
	}

	/**
	 * call this method to release the Mutex, thereby allowing a
	 * next thread to acquire the lock.
	 */
	public synchronized void release() {
		locked = false;
		notify();
	}
}
